import os
import simsym
import symtypes
import errno
import model
import signal
import ptypes
import ProcessTable
import CURRENTPROCESS
import ProcessDescr

Scheduler	= CURRENTPROCESS.CURRENTPROCESS

class UserLibrary(simsym.tstruct(
		procid		= ptypes.IPREF,
		ptab		= ProcessTable.ProcessTable,
		sched		= CURRENTPROCESS.CURRENTPROCESS,
		)):
	def _declare_assumptions(self, assume):
		super(UserLibrary, self)._declare_assumptions(assume)
	
	@model.methodwrap(ptb = ProcessTable.ProcessTable,
		 schd = Scheduler)
	def INIT(self, ptb, schd):
		self.ptab = ptb
		self.sched = schd
	
	@model.methodwrap(pprio = ptypes.PRIO,
			stkd = ptypes.PSTACK,
			datad = ptypes.PDATA,
			cdd = ptypes.PCODE,
			allocin = ptypes.MEMDESC,
			totmemsz = simsym.SInt)
	def CreateProcess(self, pprio, stkd, datad, cdd, allocin,
				totmemsz):
		stat = ptypes.pstnew
		pd = ProcessDescr.ProcessDescr.var("pd")
		pd.INIT(pprio, stat, stkd, datad, cdd, allocin, totmemsz)
		pid = ptypes.IPREF.var("pid")
		self.ptab.AddProcess(pid, pd)
		self.sched.MakeReady(pid)
		self.procid = pid
	
	@model.methodwrap()
	def TerminateProcess(self):
		self.sched.MakeUnready(self.procid)
		self.ptab.DelProcess(self.procid)
	
model_class = UserLibrary
